package com.ruoyi.web.controller.process;


import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.domain.process.ProcessInventory;
import com.ruoyi.system.domain.process.ProcessRibbon;
import com.ruoyi.system.domain.process.ProcessShunt;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.processService.IProcessInventoryService;
import com.ruoyi.system.service.processService.IProcessRibbonService;
import com.ruoyi.system.service.processService.IProcessShuntService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 分卷Controller
 * 
 * @author ljx
 * @date 2020-07-13
 */
@Controller
@RequestMapping("/process/shunt")
public class ProcessShuntController extends BaseController
{
    private String prefix = "process/shunt";

    @Autowired
    private IProcessShuntService processShuntService;
    @Resource
    private ISysUserService iSysUserService;
    @Resource
    private IProcessInventoryService iProcessInventoryService;

    @RequiresPermissions("process:shunt:view")
    @GetMapping()
    public String shunt()
    {
        return prefix + "/shunt";
    }

    /**
     * 查询分卷列表
     */
    @RequiresPermissions("process:shunt:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ProcessShunt processShunt)
    {
        startPage();
        List<ProcessShunt> list = processShuntService.selectProcessShuntList(processShunt);
        return getDataTable(list);
    }

    /**
     * 导出分卷列表
     */
    @RequiresPermissions("process:shunt:export")
    @Log(title = "分卷", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ProcessShunt processShunt)
    {
        List<ProcessShunt> list = processShuntService.selectProcessShuntList(processShunt);
        list.stream().forEach(shunt -> {
            shunt.setSurfaceName(shunt.getSurface()==1?"内":shunt.getSurface()==2?"外":"内+外");
        });
        ExcelUtil<ProcessShunt> util = new ExcelUtil<ProcessShunt>(ProcessShunt.class);
        return util.exportExcel(list, "shunt");
    }

    /**
     * 新增分卷
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        List<SysUser> users = iSysUserService.getAllUser();
        //获取到非未加工状态，非出库，非分卷绑定过的并卷,
        // 数据结构，每个ribbon对象中包含一个原膜对象集合，对应并卷锁绑定的原膜
        List<ProcessRibbon> ribbons = processShuntService.getRibbonForAdd();
        mmap.put("users",users);
        //合并材料编号
        mmap.put("ribbons",ribbons);
        return prefix + "/add";
    }

    /**
     * 新增保存分卷
     */
    @RequiresPermissions("process:shunt:add")
    @Log(title = "分卷", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ProcessShunt processShunt)
    {
        processShunt.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(processShuntService.insertProcessShunt(processShunt));
    }

    /**
     * 修改分卷
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        //获得到分卷信息+闭卷信息
        ProcessShunt processShunt = processShuntService.selectProcessShuntById(id);
        if(processShunt.getSurface()==1){
            processShunt.setSurfaceName("内");
        }else if(processShunt.getSurface()==2){
            processShunt.setSurfaceName("外");
        }else if(processShunt.getSurface()==3){
            processShunt.setSurfaceName("内+外");
        }
        //查询到原膜信息
        mmap.put("processShunt", processShunt);
        List<SysUser> users = iSysUserService.getAllUser();
        mmap.put("users",users);
        if(processShunt.getStatus()==4){
            List<ProcessRibbon> ribbons = processShuntService.getRibbonForAdd();
            List<Long> ribbonIds = ribbons.stream().map(ProcessRibbon::getId).collect(Collectors.toList());
            //加上当前这个ribbon
            //判断原膜是否全部绑定完毕
            List<ProcessInventory> objects = new ArrayList<>();
            ProcessInventory inventory = iProcessInventoryService.selectProcessInventoryById(processShunt.getInventoryId());
            objects.add(inventory);
            //未，直接将修改对象的原膜加入到集合中
            if(ribbonIds.contains(processShunt.getRibbonId())){
                ribbons.stream().forEach(r -> {
                    if(r.getId().intValue() == processShunt.getRibbonId()){
                        r.getInventories().addAll(objects);
                    }
                });
            }else {
                //全部为添加进入分卷后，新建ribbon，添加到ribbons中
                ProcessRibbon ribbon = new ProcessRibbon();
                ribbon.setId(processShunt.getRibbonId());
                ribbon.setRibbonNumber(processShunt.getRibbonNumber());
                ribbon.setInventories(objects);
                ribbons.add(ribbon);
            }
            mmap.put("ribbons",ribbons);
            return prefix + "/canEdit";
        }
        return prefix + "/edit";
    }

    /**
     * 修改保存分卷
     */
    @RequiresPermissions("process:shunt:edit")
    @Log(title = "分卷", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ProcessShunt processShunt)
    {
        processShunt.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(processShuntService.updateProcessShunt(processShunt));
    }

    /**
     * 删除分卷
     */
    @RequiresPermissions("process:shunt:remove")
    @Log(title = "分卷", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return processShuntService.deleteProcessShuntByIds(ids);
    }
}
